package com.zdkj.subway.support.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.support.entity.SwPmTvmCashboxCode;
import com.zdkj.subway.support.entity.SwPmTvmCode;
import com.zdkj.subway.support.service.SwPmTvmCashboxCodeService;
import com.zdkj.subway.support.util.UploadDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * TVM钱箱号参数表
 *
 * @author cuilei
 * @date 2020-05-28 14:03:54
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swpmtvmcashboxcode")
@Api(value = "swpmtvmcashboxcode", tags = "TVM钱箱号参数表管理")
public class SwPmTvmCashboxCodeController {

    private final SwPmTvmCashboxCodeService swPmTvmCashboxCodeService;

    /**
     * 分页查询
     *
     * @param page               分页对象
     * @param swPmTvmCashboxCode TVM钱箱号参数表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwPmTvmCashboxCodePage(Page page, SwPmTvmCashboxCode swPmTvmCashboxCode) {
        if (!SecurityUtils.getUser().getBelongToLine()) {
            QueryWrapper<SwPmTvmCashboxCode> queryWrapper = Wrappers.query(swPmTvmCashboxCode);
            queryWrapper.lambda().eq(SwPmTvmCashboxCode::getLineCode, SecurityUtils.getUser().getLineCode());
            queryWrapper.lambda().eq(SwPmTvmCashboxCode::getStationCode, SecurityUtils.getUser().getStationCode());
            return R.ok(swPmTvmCashboxCodeService.page(page, queryWrapper));
        }
        return R.ok(swPmTvmCashboxCodeService.page(page, Wrappers.query(swPmTvmCashboxCode)));
    }

    /**
     * 通过id查询TVM钱箱号参数表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swPmTvmCashboxCodeService.getById(id));
    }

    /**
     * 新增TVM钱箱号参数表
     *
     * @param swPmTvmCashboxCode TVM钱箱号参数表
     * @return R
     */
    @ApiOperation(value = "新增TVM钱箱号参数表", notes = "新增TVM钱箱号参数表")
    @SysLog("新增TVM钱箱号参数表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_tvm_cashbox_code_add')")
    public R save(@RequestBody SwPmTvmCashboxCode swPmTvmCashboxCode) {
        LambdaQueryWrapper<SwPmTvmCashboxCode> lambdaQueryWrapper=Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(SwPmTvmCashboxCode::getCashboxCode,swPmTvmCashboxCode.getCashboxCode());
        lambdaQueryWrapper.eq(SwPmTvmCashboxCode::getStationCode,swPmTvmCashboxCode.getStationCode());
        SwPmTvmCashboxCode codeswPmTvmCashboxCode=swPmTvmCashboxCodeService.getOne(lambdaQueryWrapper);
        if(ObjectUtil.isNotEmpty(codeswPmTvmCashboxCode)){
            return R.failed(codeswPmTvmCashboxCode,codeswPmTvmCashboxCode.getCashboxCode()+"已存在,TVM钱箱号不能重复添加");
        }
        SwPmTvmCashboxCode defaultObj = ObjectUtils.getDefaultObj(SwPmTvmCashboxCode.class);
        BeanUtils.copyProperties(swPmTvmCashboxCode, defaultObj,
                ObjectUtils.getNullAndEmptyPropertyNames(swPmTvmCashboxCode));
        defaultObj.setLineCode(swPmTvmCashboxCode.getLineCode());
        defaultObj.setStationCode(swPmTvmCashboxCode.getStationCode());
        return R.ok(swPmTvmCashboxCodeService.save(defaultObj));
    }

    /**
     * 修改TVM钱箱号参数表
     *
     * @param swPmTvmCashboxCode TVM钱箱号参数表
     * @return R
     */
    @ApiOperation(value = "修改TVM钱箱号参数表", notes = "修改TVM钱箱号参数表")
    @SysLog("修改TVM钱箱号参数表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_tvm_cashbox_code_edit')")
    public R updateById(@RequestBody SwPmTvmCashboxCode swPmTvmCashboxCode) {
        return R.ok(swPmTvmCashboxCodeService.updateById(swPmTvmCashboxCode));
    }

    /**
     * 通过id删除TVM钱箱号参数表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除TVM钱箱号参数表", notes = "通过id删除TVM钱箱号参数表")
    @SysLog("通过id删除TVM钱箱号参数表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_sw_pm_tvm_cashbox_code_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swPmTvmCashboxCodeService.removeById(id));
    }

    /**
     * 导入TVM钱箱号参数
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入TVM钱箱号参数", notes = "导入TVM钱箱号参数")
    @SysLog("导入TVM钱箱号参数")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SwPmTvmCashboxCode.class,
                    new UploadDataListener(swPmTvmCashboxCodeService, SwPmTvmCashboxCode.class)).sheet().doRead();
            return R.ok("导入成功");
        } catch (IOException e) {
            log.info(e.getMessage());
            return R.ok("导入失败");
        }
    }
}
